<?php

namespace App\Http\Controllers;

use App\Events\YearActivitySendCoupon;
use App\services\v1\UserServices;
use Dingo\Api\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class AuthController extends Controller
{

    /**
     * 登陆
     * @param Request $request
     * @return false|JsonResponse|string
     */
    public function login(Request $request)
    {
        $this->validate(request(),[
            'user_mobile' => 'required',
            'password'    => 'required',
        ]);

        $params = $request->only(['user_mobile', 'password']);
        if (!check_mobile($params['user_mobile'])){
            return $this->error(50022);
        }

        if (!$token = auth('api')->attempt($params)) {
            return $this->error(10002);
        }

        //todo other

        $data = [
            'token'         => $token,
            'expire_in'     => auth('api')->factory()->getTTL(),
            'token_type'    => 'Bearer',
        ];
        //新年送券活动
        event(new YearActivitySendCoupon(Auth::user()));
        return  $this->success($data);
    }

    /**
     * register a new user
     * @param Request $request
     * @return false|string
     */
    public function register(Request $request)
    {
        $userMobile = $request->get('mobile');
        $userEmail  = $request->get('email');

        $validator  = validator(
            $request->all(),
            [
                'mobile'        => 'required',
                'email'         => 'required|email',
            ],
            [
                'mobile.required'  => trans('errorCode.50022'),
                'email.required'   => trans('errorCode.50039'),
                'email.email'      => trans('errorCode.50038'),
            ]
        );

        if ($validator->fails()) {
            return $this->error(40001,$validator->errors()->first());
        }

        //todo 手机验证码

        if (!check_mobile($userMobile)){
            return $this->error(50022);
        }

        $userService = new UserServices();
        // todo rr级别下有并发问题 结合业务考虑是否加X锁
        if($userService->getUserByMobile($userMobile, $userEmail)){
            return $this->error(50020);
        }

        $res = $userService->setUser($userMobile,$userEmail);

        return  $this->success([],'',$res['code']);
    }

    /**
     * logout
     * @return false|string
     */
    public function logout()
    {
        auth('api')->logout();
        
        return $this->success([],'',10007);
    }

    /**
     * Refresh a token.
     *
     * @return false|string
     */
    public function refresh()
    {
        $newToken = '';

        if ($oldToken = auth('api')->getToken()) {
            $newToken = auth('api')->refresh($oldToken);

            //jwt.php配置默认是true
            auth('api')->invalidate($oldToken);
        }

        $data = [
            'token'         => $newToken,
            'expire_in'     => auth('api')->factory()->getTTL(),
            'token_type'    => 'Bearer',
        ];


        return $this->success($data);
    }

}